var map;
function initialize() {
    map = new BMapGL.Map('map');
    map.centerAndZoom(new BMapGL.Point(109.419105, 24.324407), 14);
    map.enableScrollWheelZoom(true);

    var navi3DCtrl = new BMapGL.NavigationControl3D();  // 添加3D控件
    map.addControl(navi3DCtrl);
    $(".anchorBL").remove();


}
function huizhi() {
    //获取被选中的option标签
    var neirong = $('select  option:selected').val();

    if (neirong == 'dian') {
        removeMapEvent();
        map.addEventListener('click', function (e) {
            var point = new BMapGL.Point(e.latlng.lng, e.latlng.lat);
            var marker = new BMapGL.Marker(point);        // 创建标注
            map.addOverlay(marker);
			      removeMapEvent();
			layui.use('layer', function(){
			  var layer = layui.layer;
				 layer.open({
					 type: 1,
					 shade: 0,
					 area: ['800px', '800px'],
					 size:0,
					 title: '填写地质灾害信息',
					 content: $('#damageform') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
					 });
			});  
			

        });
			


    /* 
		 layer.open({
		 type: 1,
		 shade: 0,
		 area: ['700px', '300px'],
		 title: '填写地质灾害信息',
		 content: $('#damageform') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
		 });
		 */
		 
		 
    } 
	
	
}
// 移除地图点击事件
function removeMapEvent() {
    map.removeEventListener('click');
}

layui.use('upload', function(){
  var upload = layui.upload;
  //执行实例
  var uploadInst = upload.render({
    elem: '#uploadPic' //绑定元素
    ,url: '/upload/uploadFile'//上传接口
      ,accept: 'images',
       done: function(res){
             layui.use('layer', function(){
	
	                $("#uploaded-files").append(
	                    $("<tr id='option" + res.list[0].id + "' data-id='" +  res.list[0].id + "'/>")
						 .append($('<td/>').html("<img class='layui-upload-img' id='pic" + res.list[0].id + "' alt='"+res.list[0].fileName+"' src='/upload/downloadFile/" + res.list[0].id +"'  >"))
	               
	                        .append($('<td/>').html("<a onclick='deleteRow(" + res.list[0].id + ")'>删除</a>"))
	                );
	       	
               });
       }   ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, file, result){
        $('#demo1').attr('src', result); //图片链接（base64）
      });
    }
       ,error: function(err){
	
		   console.log(err)
         //请求异常回调
       }
  });
});
function deleteRow(id) {
    $("#option" + id).remove();
}
function getAttachmentIds() {
    var ids = new Array();
    $("#uploaded-files tr").each(function () {
        ids.push($(this).attr("data-id"));
    });
    return ids;
}




layui.use('form', function(){
  var form = layui.form;
  
  //监听提交
  form.on('submit(damageform)', function(data){
	 
/*    layer.msg(JSON.stringify(data.field)); */
	    //所有附件id集合
	    var attaIds = getAttachmentIds();

	    var dzzh = {
			/* 整合:后期要加 createdUser 和坐标属性 */
	        "xzq": data.field.xzq,
	        "createdType": data.field.lx,
 	        "createdTime": data.field.time
	    };
		var dzzhdata=JSON.stringify(dzzh);
		var attas=JSON.stringify(attaIds);
		

		
	    $.ajax({
	         type: "post",
	          dataType: "json",
			  url: "/dzzh/saveDzzh",
	        data:{"dzzh":dzzhdata,"attaIds":attas},
             contentType: "application/x-www-form-urlencoded;charset=UTF-8",
			  async: false,
	        success: function (data) {
		
	        if(data==true){
		
				 layer.msg('保存成功!', {
				   icon: 1,
				   time: 5000 //2秒关闭（如果不配置，默认是3秒）
				 }, function(){
				 		layer.closeAll(); 
				 }); 
				 
				 
				 
				layer.closeAll(); 
			}else{
				 layer.msg("保存失败!");
			}
				
	
	        },
	        error: function (data) {
			 layer.msg("保存失败!");
	       
	        }
	    }); 
	
	
 return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
  });
});